package bma.common.netty.telnetserver;

import org.junit.Before;
import org.junit.Test;

import bma.common.langutil.core.ObjectUtil;
import bma.common.langutil.jmshell.Session;
import bma.common.langutil.jmshell.cmdopt.CommandOptionParser;
import bma.common.langutil.jmshell.command.AbstractSimpleCommandProcessor;
import bma.common.langutil.jmshell.command.JMShellCommandGroup;
import bma.common.langutil.jmshell.command.JMShellModule;
import bma.common.langutil.jmshell.common.JMShellProcessor4Echo;
import bma.common.langutil.jmshell.common.JMShellProcessor4Link;
import bma.common.langutil.jmshell.common.JMShellProcessor4Miss;
import bma.common.langutil.jmshell.common.JMShellProcessor4SessionClose;
import bma.common.langutil.jmshell.impl.JMShell;
import bma.common.langutil.log.LogbackUtil;

public class NettyTelnetServerTC {

	@Before
	public void setUp() {
		LogbackUtil.setLevel("bma.common.langutil.concurrent.ProcessTimeWheel",
				"INFO");
	}

	public NettyTelnetServer server() {
		NettyTelnetServer s = new NettyTelnetServer();
		s.setPort(1080);
		s.setName("TelnetServer");
		s.setTraceBufferSize(1024);
		return s;
	}

	@Test
	public void server_simple() {

		JMShellProcessor4Link p = new JMShellProcessor4Link();
		p.addProcessor(new JMShellProcessor4SessionClose());
		p.addProcessor(new JMShellProcessor4Echo());

		NettyTelnetServer s = server();
		s.setProcessor(p);
		s.start();
		ObjectUtil.waitFor(this, 60 * 1000);
		s.close();
	}

	@Test
	public void server_command() {

		JMShellCommandGroup p = new JMShellCommandGroup();
		p.addProcessor(new JMShellProcessor4SessionClose());
		p.addProcessor(new AbstractSimpleCommandProcessor("test") {

			@Override
			public boolean processCommand(Session session, String cmd,
					CommandOptionParser cmdObject) {
				session.writeln("hello from test - "
						+ cmdObject.getOptionValue("debug", Boolean.FALSE));
				return true;
			}

			@Override
			public CommandOptionParser createCommandOption(Session session) {
				CommandOptionParser co = new CommandOptionParser();
				co.addBooleanOption('d', "debug", "enable debug");
				return co;
			}
		});
		p.addProcessor(new JMShellProcessor4Miss());

		NettyTelnetServer s = server();
		s.setProcessor(p);
		s.start();
		ObjectUtil.waitFor(this, 60 * 1000);
		s.close();
	}

	@Test
	public void server_module() {

		JMShellModule p = new JMShellModule();
		p.addProcessor("close", new JMShellProcessor4SessionClose());
		p.addProcessor("cmd", new AbstractSimpleCommandProcessor("test") {

			@Override
			public boolean processCommand(Session session, String cmd,
					CommandOptionParser cmdObject) {
				session.writeln("hello from test - "
						+ cmdObject.getOptionValue("debug", Boolean.FALSE));
				return true;
			}

			@Override
			public CommandOptionParser createCommandOption(Session session) {
				CommandOptionParser co = new CommandOptionParser();
				co.addBooleanOption('d', "debug", "enable debug");
				return co;
			}
		});

		NettyTelnetServer s = server();
		s.setProcessor(p);
		s.start();
		ObjectUtil.waitFor(this, 60 * 1000);
		s.close();
	}

	@Test
	public void server_shell() {

		JMShell shell = new JMShell();
		shell.init();

		NettyTelnetServer s = server();
		s.setProcessor(shell);
		s.start();
		ObjectUtil.waitFor(this, 60 * 1000);
		s.close();
	}
}
